//
//  KAAdvanceFail.swift
//  kano
//
//  Created by Frank on 2024/11/27.
//

import UIKit

class KAAdvanceFail: KABaseVC {
    
    var applyModel: KAApply?
    var basicModel: KABasic?
    
    lazy var var_resultImg: UIImageView = {
        let var_resultImg = UIImageView(image: UIImage(named: "apply_success"))
        return var_resultImg
    }()
    
    public var symbolLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: kscale(16), weight: .bold)
        label.textColor = .red
        label.textAlignment = .center
        label.text = ""
        return label
    }()
    lazy var netTip: UIButton = {
        let button = UIButton()
        button.setTitle(" Please make sure a good network connection", for: .normal)
        button.setTitleColor(.color8A8891, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(12))
        button.setImage(UIImage(named: "liveness_netTip"), for: .normal)
        return button
    }()
    lazy var lightTip: UIButton = {
        let button = UIButton()
        button.setTitle(" Please make sure a good lighting in your surroundings", for: .normal)
        button.setTitleColor(.color8A8891, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(12))
        button.setImage(UIImage(named: "liveness_lightTip"), for: .normal)
        return button
    }()
    lazy var phoneTip: UIButton = {
        let button = UIButton()
        button.setTitle(" Please face the phone directly", for: .normal)
        button.setTitleColor(.color8A8891, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(12))
        button.setImage(UIImage(named: "liveness_phoneTip"), for: .normal)
        return button
    }()
    lazy var timeTip: UIButton = {
        let button = UIButton()
        button.setTitle(" During recognition, please finish quickly", for: .normal)
        button.setTitleColor(.color8A8891, for: .normal)
        button.titleLabel?.font = kFontSize(kscale(12))
        button.setImage(UIImage(named: "liveness_timeTip"), for: .normal)
        return button
    }()
    lazy var var_homeBtn: UIButton = {
        let button = UIButton()
        button.setTitle("Retry", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: kscale(17), weight: .bold)
        button.backgroundColor = .themeColor
        button.layer.cornerRadius = kscale(25)
        button.layer.masksToBounds = true
        button.addTarget(self, action: #selector(ka_homeBtnClick), for: .touchUpInside)
        return button
    }()
    @objc func ka_homeBtnClick() {
        let btnInfo = KABtnInfo(eleId: "alternate_liveness_retry", pageId: "original_liveness_unsuccessful", clickTime: Date.ka_currentData())
        KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
        
        KAAdvance.shared.setData(model: applyModel, basic: basicModel)
        KAAdvance.shared.getAccessToken()
    }
    
    override func customNavBackAction() {
        for vc in navigationController!.viewControllers {
            if vc.isKind(of: KAInfoVC.self) {
                let infoVC = vc as! KAInfoVC
                navigationController?.popToViewController(infoVC, animated: true)
                return
            }
        }
        navigationController?.popViewController(animated: true)
    }
        
    
    init(model: KAApply?, basic: KABasic?) {
        applyModel = model
        basicModel = basic
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension KAAdvanceFail {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        let data = KAPageInfo(eleId: "original_liveness_unsuccessful", path: "\(KAAdvanceFail.self)")
        KAReport.ka_reportWith(type: .page, data: data.kj.JSONObject())
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ka_setUI()
    }

    func ka_setUI() {
        
        view.backgroundColor = .colorf2f2f5
        view.addSubview(var_resultImg)
        view.addSubview(netTip)
        view.addSubview(lightTip)
        view.addSubview(phoneTip)
        view.addSubview(timeTip)
        view.addSubview(var_homeBtn)
        view.addSubview(symbolLabel)
        var_resultImg.image = UIImage(named: "liveness_fail")
        symbolLabel.text = "Face Recognition Fail"
   
        
        ka_showCustomerNavBar(type: .normal)
        var_navBar.var_titleLabel.text = "Profile Verification"
        
        var_resultImg.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: kscale(120), height: kscale(120)))
            make.top.equalTo(UIDevice.ka_fullNavHeight+kscale(32))
            make.centerX.equalTo(view)
        }
        symbolLabel.snp.makeConstraints { make in
            make.left.equalTo(view).offset(kscale(20))
            make.right.equalTo(view).offset(kscale(-20))
            make.top.equalTo(var_resultImg.snp.bottom).offset(kscale(18))
        }
        
        netTip.snp.makeConstraints { make in
            make.top.equalTo(symbolLabel.snp.bottom).offset(kscale(32))
            make.centerX.equalTo(view)
        }
        lightTip.snp.makeConstraints { make in
            make.left.equalTo(netTip.snp.left)
            make.top.equalTo(netTip.snp.bottom).offset(kscale(10))
        }
        phoneTip.snp.makeConstraints { make in
            make.left.equalTo(netTip.snp.left)
            make.top.equalTo(lightTip.snp.bottom).offset(kscale(10))
        }
        timeTip.snp.makeConstraints { make in
            make.left.equalTo(netTip.snp.left)
            make.top.equalTo(phoneTip.snp.bottom).offset(kscale(10))
        }
        var_homeBtn.snp.makeConstraints { make in
            make.left.equalTo(view).offset(kscale(20))
            make.right.equalTo(view).offset(kscale(-20))
            make.top.equalTo(timeTip.snp.bottom).offset(kscale(180))
            make.height.equalTo(kscale(50))
        }
    }
}
